//
//  YHY_MyPageSearch_TVC.m
//  TTMusic
//
//  Created by yanghuiyan on 16/4/3.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "YHY_MyPageSearch_TVC.h"
#import "MyPageSearchCollectionViewCell.h"
@implementation YHY_MyPageSearch_TVC

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.dataSource = [NSMutableArray array];
        self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
        self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 1, 0, 0);
        
        self.flowLayout.minimumLineSpacing = 1;
        self.flowLayout.minimumInteritemSpacing = 0;
        self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_flowLayout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerClass:[MyPageSearchCollectionViewCell class] forCellWithReuseIdentifier:@"1"];
        [self.contentView addSubview:self.collectionView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.collectionView.frame = self.contentView.bounds;

    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UILabel *label = [UILabel new];
    label.numberOfLines = 0;
    DataModel *datadModel = _dataSource[indexPath.item];
    label.text = datadModel.val;
    //    设置字体以及大小的字典
    NSDictionary *fontDic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName];
    //    设置设置高度和宽度
    CGSize size = [label.text boundingRectWithSize:CGSizeMake(1000,40) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:fontDic context:nil].size;
    
    return CGSizeMake(size.width + 11, 40);
    
}

#pragma mark - UICollectionViewDelegate协议方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyPageSearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"1" forIndexPath:indexPath];
    DataModel *dataModel = _dataSource[indexPath.item];
    cell.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:249 / 255.0 blue:240 / 255.0 alpha:1];
    cell.layer.cornerRadius = 5;
    cell.layer.masksToBounds = YES;
    cell.label.text = dataModel.val;
    cell.label.textColor = [UIColor grayColor];
    return cell;
}
- (void)setDataSource:(NSMutableArray *)dataSource {
    _dataSource = dataSource;
    [_collectionView reloadData];
}
- (void)setDataModel:(DataModel *)dataModel {
    
    _dataModel = dataModel;
    
    _string = dataModel.val;
}

/// 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   
    // 发送通知
    DataModel *data = self.dataSource[indexPath.row];
    NSString *string = data.val;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"detail" object:string];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
